package ntu.khpi.eim30.project.x;

public class LiFSR {

	private int N;
	private int[] reg;
	private int tap;

	public LiFSR(String seed, int t) {

		this.tap = t;
		N = seed.length();
		reg = new int[N];
		for (int i = 0; i < N; i++) {
			reg[i] = seed.charAt(i) - 48; /* ASCII value to produce 0 or 1. */
		}

	}

	public int step() {

		int nullPosition = reg[0];
		int positionStep = reg[N - (tap + 1)];
		int xorPoint = nullPosition ^ positionStep;
		for (int i = 0; i < N; i++) {
			if (i != N - 1) {
				reg[i] = reg[i + 1]; // Shift one position.
			} else
				reg[i] = xorPoint;
		}
		return xorPoint;
	}

	public int generate(int k) {
		int j = 0;
		for (int i = 0; i < k; i++) {
			int b = step();
			j = j * 2 + b;
		}
		return j;
	}

	public String toString() {

		String st = "";
		for (int i = 0; i < N; i++) {
			st = st + reg[i];
		}
		return st;
	}

	

}